//
//  LookPhotoViewController.m
//  XiangJi
//
//  Created by apple on 2017/8/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "LookPhotoViewController.h"
#import "LookPhotoCollectionViewCell.h"
#define CELL_ID @"cellID"
@interface LookPhotoViewController ()

@property (nonatomic, strong) ZLPhotoActionSheet *sheet;

@property (nonatomic, strong) NSMutableArray *loginMutArr;

@end

@implementation LookPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.loginMutArr = [NSMutableArray arrayWithCapacity:0];

    //使用collectview呈现图片
    [self createCollectView];
}



#pragma 从相册中取下资源
- (void)createPhoto{
    
    self.sheet = [[ZLPhotoActionSheet alloc] init];
    
    self.sheet.sender = self;

    [self.sheet showPhotoLibrary];
}

//创建collectview
- (void)createCollectView{

    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
  
    self.flowLayout.itemSize = CGSizeMake(90*Scale_Ratio, 125*Scale_Ratio);

    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.lookCollectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    
    self.lookCollectView.delegate = self;
    self.lookCollectView.dataSource = self;
    
    self.lookCollectView.backgroundColor = [UIColor whiteColor];
    
    [self.lookCollectView registerClass:[LookPhotoCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
    
    [self.view addSubview:self.lookCollectView];
    
    [self.lookCollectView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.bottom.right.equalTo(self.view);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    //1：代表最后一张图片 即，添加照片的那张图
    return _lookMutArr.count+1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"indes = %ld",indexPath.row);
    
    //进来的时候，全部清空
    [self.loginMutArr removeAllObjects];
    
    //当时最后一个item的时候，进入相册
    if (indexPath.item == _lookMutArr.count) {
        
        //进入相册
        [self createPhoto];
        
        __weak typeof(self) weakSelf = self;
        
        //相册中选取图片调用方法
        [self.sheet setSelectImageBlock:^(NSArray<UIImage *> * images, NSArray<PHAsset *> * assets, BOOL isOriginal) {
            
            [weakSelf.loginMutArr addObjectsFromArray:images];

            //for循环将选中的图片加入数组里
            for (int l = 0; l < images.count; l++) {
                
                //判断图片是否是重复的
                if (![weakSelf.lookAssetsMutArr containsObject:assets[l]]) {
                    
                    //若是不重复，则将图片加入数组
                    [weakSelf.lookMutArr addObject:weakSelf.loginMutArr[l]];
                    
                    //将图片的信息加入判断是否重复的数组中
                    [weakSelf.lookAssetsMutArr addObject:assets[l]];
                    
                }else{
                
                    continue;
                }
            }
         
            //刷新
            [weakSelf.lookCollectView reloadData];
            
        }];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LookPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
    
    if (indexPath.item == _lookMutArr.count) {
        
        cell.cellImageView.image = [UIImage imageNamed:@"+@2x.png"];

        //隐藏叉号
        cell.cellBtn.hidden = YES;
        
        cell.bgView.hidden = YES;
        
    }else{
        
        cell.cellImageView.image = _lookMutArr[indexPath.row];
        
        //一定得加这个 - 不然你会发现图片上的叉号乱窜 [捂脸]
        cell.cellBtn.hidden = NO;
        cell.bgView.hidden = NO;
    }
    
    
    

    [cell.cellBtn setImage:[UIImage imageNamed:@"shanchu@2x.png"] forState:UIControlStateNormal];
    
    [cell.cellBtn addTarget:self action:@selector(cellBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.cellBtn.tag = indexPath.row;
    
    
    
    return cell;
}

//设置格子上左下右间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20*Scale_Ratio, 32*Scale_Ratio, 0, 32*Scale_Ratio);
}


//btn点击事件
- (void)cellBtnClick:(UIButton *)sender{
    
    NSLog(@"cellBtn = %ld",sender.tag);
    //取出源item数据
    id objc = [self.lookMutArr objectAtIndex:sender.tag];
    //从资源数组中移除该数据
    [self.lookMutArr removeObject:objc];
    [self.lookCollectView reloadData];

}



@end
